package com.yhsimon.base;

import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

public class TestTX {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "YHSimon");
        jsonObject.put("age",18);
        String result = jsonObject.toJSONString();
        jedis.flushDB();
        //开启事务
        Transaction multi = jedis.multi();
        try {
            multi.set("user1", result);
            multi.set("user2",result);
            // int i=1/0; //代码抛出异常事务，执行失败
            multi.exec();  //执行事务
        } catch (Exception e) {
            multi.discard(); //抛弃事务 相当于回滚
            e.printStackTrace();
        } finally {
            System.out.println(jedis.get("user1"));
            System.out.println(jedis.get("user2"));

            jedis.close(); //关闭连接
        }
    }
}
